package super

import (
	"Project/global"
	Area "Project/model/area"
	"Project/model/area/common"
	"Project/utils"
)

type AreaService struct{}

func (area *AreaService) GetAreaList() ([]common.AreaInfo, error) {
	var data []common.AreaInfo
	if err := global.GVA_DB.Debug().Model(&Area.Area{}).Select("area_id", "name").Find(&data).Error; err != nil {
		return nil, err
	}
	return data, nil
}

func (area *AreaService) GetAreaIdList() ([]int64, error) {
	var idList []int64
	if err := global.GVA_DB.Debug().Model(&Area.Area{}).Select("area_id").Find(&idList).Error; err != nil {
		return nil, err
	}
	return idList, nil
}

func (area *AreaService) DeleteById(areaId string) error {
	if err := global.GVA_DB.Debug().Model(&Area.Area{}).Where("area_id=?", areaId).Delete(&Area.Area{}).Error; err != nil {
		return err
	}
	return nil
}

func (area *AreaService) UpdateArea(name, areaId string) error {
	if err := global.GVA_DB.Debug().Model(&Area.Area{}).Where("area_id=?", areaId).Update("name", name).Error; err != nil {
		return err
	}
	return nil
}

func (a *AreaService) CreateArea(name string) error {
	if err := global.GVA_DB.Debug().Model(&Area.Area{}).Create(&Area.Area{
		AreaID: utils.GenID(),
		Name:   name,
	}).Error; err != nil {
		return err
	}
	return nil
}
